package com.flx.dis.registry.zk.server;

import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Fenglixiong
 * @Create 2022/4/19 15:15
 * @Description
 **/
@Slf4j
@Component
public class ZkServer {

    private ZkClient zkClient;



    public ZkClient getZkClient(){
        return zkClient;
    }

    /**
     * 连接zk
     * @param ip ip地址
     * @param port zk端口
     * @throws Exception
     */
    public void connect(String ip,String port)throws Exception{
        close();
        zkClient = new ZkClient(ip+":"+port,40000,40000);
        System.out.println("zk连接成功...");
    }

    /**
     * 关闭zk
     * @throws Exception
     */
    public void close()throws Exception{
        if(zkClient!=null) {
            zkClient.close();
            zkClient = null;
        }
        log.info("关闭zk成功...");
    }

    /**
     * 创建持久节点
     * @param path 路径
     * @param data 节点数据
     * @throws Exception
     */
    public void createPersistent(String path,String data)throws Exception{
        zkClient.createPersistent(path,data);
    }

    /**
     * 创建临时节点
     * @param path 路径
     * @param data 节点数据
     * @throws Exception
     */
    public void createEphemeral(String path,String data)throws Exception{
        zkClient.createEphemeral(path,data);
    }

    /**
     * 创建临时序号节点
     * @param path 路径
     * @param data 节点信息
     * @throws Exception
     */
    public String createEphemeralSequential(String path,String data)throws Exception{
        return zkClient.createEphemeralSequential(path,data);
    }

    /**
     * 读取节点数据
     * @param path 路径
     * @return
     * @throws Exception
     */
    public String getData(String path)throws Exception{
        return zkClient.readData(path);
    }

    /**
     * 设置节点数据
     * @param path
     * @param data
     * @throws Exception
     */
    public void setData(String path,String data)throws Exception{
        zkClient.writeData(path,data);
    }

    /**
     * 删除节点
     * @param path
     * @throws Exception
     */
    public boolean delete(String path)throws Exception{
        return zkClient.delete(path);
    }

    /**
     * 包含子节点一起删除
     * @param path
     * @return
     * @throws Exception
     */
    public boolean deleteAll(String path)throws Exception{
        return zkClient.deleteRecursive(path);
    }

    /**
     * 获取节点下的子节点
     * @param path 父类节点
     * @return 全部子节点
     * @throws Exception
     */
    public List<String> getChildren(String path)throws Exception{
        return zkClient.getChildren(path);
    }

    /**
     * 路径是否存在
     * @param path 节点
     * @return 是否存在
     * @throws Exception
     */
    public boolean exists(String path)throws Exception{
        return zkClient.exists(path);
    }

    /**
     * 订阅节点数量变化
     * @param path 路径
     * @param listener 监听器
     * @return 路径下节点
     * @throws Exception
     */
    public List<String> subscribeChildChange(String path, IZkChildListener listener)throws Exception{
        return zkClient.subscribeChildChanges(path,listener);
    }

    /**
     * 订阅数据改变事件
     * @param path 路径
     * @param listener 数据变化监听器
     * @throws Exception
     */
    public void subscribeDataChange(String path, IZkDataListener listener)throws Exception{
        zkClient.subscribeDataChanges(path,listener);
    }

    /**
     * 取消节点监听
     * @param path
     * @param listener
     * @throws Exception
     */
    public void unsubscribeChildChange(String path,IZkChildListener listener)throws Exception{
        zkClient.unsubscribeChildChanges(path,listener);
    }

    /**
     * 取消节点数据改变监听
     * @param path
     * @param listener
     * @throws Exception
     */
    public void unsubscribeDataChange(String path,IZkDataListener listener)throws Exception{
        zkClient.unsubscribeDataChanges(path,listener);
    }

    /**
     * 取消所有监听事件
     * @throws Exception
     */
    public void unsubscribeAll()throws Exception{
        zkClient.unsubscribeAll();
    }

}
